package com.entmanage.base.hrm.controller;

import com.entmanage.base.hrm.domain.HrmEmployee;
import com.entmanage.base.hrm.service.IHrmEmployeeService;
import com.entmanage.common.annotation.Log;
import com.entmanage.common.core.controller.BaseController;
import com.entmanage.common.core.domain.AjaxResult;
import com.entmanage.common.core.domain.entity.SysDept;
import com.entmanage.common.core.page.TableDataInfo;
import com.entmanage.common.enums.BusinessType;
import com.entmanage.common.utils.poi.ExcelUtil;
import com.entmanage.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 员工基本信息主Controller
 *
 * @author lsq
 * @date 2025-07-24
 */
@Api(tags = {"hrm-HrmEmployee模块"}, description = "员工基本信息主")
@RestController
@RequestMapping("/hrm/employee")
public class HrmEmployeeController extends BaseController
{
    @Autowired
    private IHrmEmployeeService hrmEmployeeService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询员工基本信息主列表
     */
    @PreAuthorize("@ss.hasPermi('hrm:employee:list')")
    @GetMapping("/list")
    public TableDataInfo list(HrmEmployee hrmEmployee)
    {
        startPage();
        List<HrmEmployee> list = hrmEmployeeService.selectHrmEmployeeList(hrmEmployee);
        return getDataTable(list);
    }

    /**
     * 导出员工基本信息主列表
     */
    @PreAuthorize("@ss.hasPermi('hrm:employee:export')")
    @Log(title = "员工基本信息主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HrmEmployee hrmEmployee)
    {
        List<HrmEmployee> list = hrmEmployeeService.selectHrmEmployeeList(hrmEmployee);
        ExcelUtil<HrmEmployee> util = new ExcelUtil<HrmEmployee>(HrmEmployee.class);
        util.exportExcel(response, list, "员工基本信息主数据");
    }

    /**
     * 获取员工基本信息主详细信息
     */
    @PreAuthorize("@ss.hasPermi('hrm:employee:query')")
    @GetMapping(value = "/{empId}")
    public AjaxResult getInfo(@PathVariable("empId") Long empId)
    {
        return success(hrmEmployeeService.selectHrmEmployeeByEmpId(empId));
    }

    /**
     * 新增员工基本信息主
     */
    @PreAuthorize("@ss.hasPermi('hrm:employee:add')")
    @Log(title = "员工基本信息主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HrmEmployee hrmEmployee)
    {
        return toAjax(hrmEmployeeService.insertHrmEmployee(hrmEmployee));
    }

    /**
     * 修改员工基本信息主
     */
    @PreAuthorize("@ss.hasPermi('hrm:employee:edit')")
    @Log(title = "员工基本信息主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HrmEmployee hrmEmployee)
    {
        return toAjax(hrmEmployeeService.updateHrmEmployee(hrmEmployee));
    }

    /**
     * 删除员工基本信息主
     */
    @PreAuthorize("@ss.hasPermi('hrm:employee:remove')")
    @Log(title = "员工基本信息主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{empIds}")
    public AjaxResult remove(@PathVariable Long[] empIds)
    {
        return toAjax(hrmEmployeeService.deleteHrmEmployeeByEmpIds(empIds));
    }

    /**
     * 获取部门树列表
     */
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/deptTree")
    public AjaxResult deptTree(SysDept dept)
    {
        return success(deptService.selectDeptTreeList(dept));
    }

    @GetMapping("/getByUserId/{userId}")
    public AjaxResult getByUserId(@PathVariable Long userId) {
        HrmEmployee employee = hrmEmployeeService.selectEmployeeByUserId(userId);
        if (employee != null) {
            return success(employee);
        } else {
            return AjaxResult.error("未找到员工信息");
        }
    }
}
